#include "AnimExecutor.h"

AnimExecutor::AnimExecutor()
{
    CurrentFrame = 0;
    FrameInc = 1;
    currentAnimation = NULL;
    isAnimating = false;
 
    FrameRate = 100; //Milliseconds
    OldTime = SDL_GetTicks();
}

void AnimExecutor::OnAnimate()
{
    if(currentAnimation == NULL || OldTime + FrameRate > SDL_GetTicks())
    {
        return;
    }
 
    OldTime = SDL_GetTicks();
 
    CurrentFrame += FrameInc;
 
    if(currentAnimation->flags & ANIMATION_FLAG_OSCILLATES)
    {
        if(FrameInc > 0)
        {
            if(CurrentFrame >= currentAnimation->MaxFrames)
            {
                FrameInc = -FrameInc;
            }
        }
        else
        {
            if(CurrentFrame <= 0)
            {
                FrameInc = -FrameInc;
            }
        }
    }
    else
    {
        if(CurrentFrame >= currentAnimation->MaxFrames)
        {
            if(currentAnimation->flags & ANIMATION_FLAG_ONE_TIME)
            {   
                isAnimating = false;
                return;
            }
                
            CurrentFrame = 0;
        }
    }
}
 
int AnimExecutor::GetCurrentFrame()
{
    return CurrentFrame;
}

const std::string AnimExecutor::getAnimationName()
{
    if(currentAnimation == NULL)
    {
        return "FUCK OFF IT'S NOT HERE";
    }
    
    return currentAnimation->AnimName;
}

int AnimExecutor::getAnimationCol()
{
    if(currentAnimation == NULL)
    {
        return -1;
    }
    return currentAnimation->ImageColumn;
}

void AnimExecutor::SetAnimation(const Animation& a, AnimSpeeds FrameRate)
{
    CurrentFrame = 0;
    currentAnimation = &a;
    this->FrameRate = FrameRate;
    isAnimating = true;
}